<?php
/* 
 * class.favor.php
 * 我的家园-用户收藏
 * @author samuel
 * @date 2009-05-25
 */
include_once 'class.myhomeBase.php';
class myhome_favor extends myhomeBase {

    function  __construct($core) {
        parent::__construct();
        //参数处理
        $this->step = $this->params['step'] ? strtolower(trim($core->params['step'])) : 'index';
	    $this->user_id = $this->params['uid'] ? (int)$this->params['uid'] : 1;
	    $this->type = $this->params['type'] ? strtolower(trim($this->params['type'])) : 'all';
        $this->page = $this->params['page'] ? (int)$this->params['page'] : 1;
        $this->cid = $this->params['cid'] ? (int)$this->params['cid'] : 1;
        $this->aid = $this->params['aid'] ? (int)$this->params['aid'] : 1;
	    if(!$this->user_id){
		    $this->error = "用户ID不合法！";
		    $this->jumpError();
		    exit;
	    }

        $run = "run_" . $this->step;
        
        //header("Content-type: text/vnd.wap.wml");
        $this->$run();
    }

    function run_index(){
    	//WAP页面
	    //header("Content-type: text/vnd.wap.wml");
	    //TITLE
	    $page_title = "我的家园-用户收藏";
	    $content = "";
	    
	    
	    $content .= "<img src='' alt='头像' /><br/>";
	    $content .= "<a href='".$_SERVER['PHP_SELF']."?uid=".$this->user_id."'>动态</a>｜<a href='".$_SERVER['PHP_SELF']."?act=userinfo&amp;uid=".$this->user_id."'>信息</a>｜<a href='".$_SERVER['PHP_SELF']."?act=userset&amp;uid=".$this->user_id."'>设置</a><br/>";
	$content .= "<a href='".$_SERVER['PHP_SELF']."?act=msg&amp;uid=".$this->user_id."'>消息</a>｜<a href='".$_SERVER['PHP_SELF']."?act=favor&amp;uid=".$this->user_id."'>收藏</a>｜<a href='".$_SERVER['PHP_SELF']."?act=friend&amp;uid=".$this->user_id."'>好友</a><br/>";
	$content .= "<a href='".$_SERVER['PHP_SELF']."?act=pubSeller&amp;uid=".$this->user_id."'>发布商家</a>｜<a href='".$_SERVER['PHP_SELF']."?act=pubDiscount&amp;uid=".$this->user_id."'>发布优惠</a><br/>";
	
	    $userCustomeInfo = $this->getUserCustomeInfo($this->user_id);
	    
	    //商家大类
	    if($userCustomeInfo['CustomtypeIs']){
	        $Customtype = $userCustomeInfo['Customtype'];
	        if($Customtype){
	            $arr_cids = explode(',', $Customtype);
	            $i = 1;
	            foreach($arr_cids as $cid){
	                if($i > 3) break;
	                $cate = $this->core->load('seller::sellerBase')->getSellerCateInfo($cid);
	                $content .= "<a href='".$_SERVER['PHP_SELF']."?act=favor&amp;uid=".$this->user_id."&amp;type=topclass&amp;cid=".$cate['SellerClassID']."'>".$cate['SellerClassName']."</a>|";
	                $i++;
	            }
	        }
	    }
        $content .= "<br/>";
	    //地区
	    if($userCustomeInfo['CustomqyIs']){
	        $Customqy = $userCustomeInfo['Customqy'];
	        if($Customqy){
	            $arr_ids = explode(',', $Customqy);
	            $i = 1;
	            foreach($arr_ids as $id){
	                if($i > 3) break;
	                $info = $this->core->load('seller::sellerBase')->getSellerAreaInfo($id);
	                $content .= "<a href='".$_SERVER['PHP_SELF']."?act=favor&amp;uid=".$this->user_id."&amp;type=area&amp;aid=".$info['SellerAreaID']."'>".$info['SellerAreaNameCN']."</a>|";
	                $i++;
	            }
	        }
	    }
	    $content .= "<br/>";
	    //商圈
	    if($userCustomeInfo['CustomsqIs']){
	        $Customsq = $userCustomeInfo['Customsq'];
	        if($Customsq){
	            $arr_ids = explode(',', $Customsq);
	            $i = 1;
	            foreach($arr_ids as $id){
	                if($i > 3) break;
	                $info = $this->core->load('seller::sellerBase')->getSellerCBDInfo($id);
	                $content .= "<a href='".$_SERVER['PHP_SELF']."?act=favor&amp;uid=".$this->user_id."&amp;type=cbd&amp;cid=".$info['SellerCBDID']."'>".$info['SellerCBDNameCN']."</a>|";
	                $i++;
	            }
	        }
	    }
	    $content .= "<br/>";
	    //餐饮类
	    if($userCustomeInfo['CustomCytypeIs']){
	        $CustomCytype = $userCustomeInfo['CustomCytype'];
	        if($CustomCytype){
	            $arr_ids = explode(',', $CustomCytype);
	            $i = 1;
	            foreach($arr_ids as $id){
	                if($i > 3) break;
	                $info = $this->core->load('seller::sellerBase')->getSellerCateInfo($id);
	                $content .= "<a href='".$_SERVER['PHP_SELF']."?act=favor&amp;uid=".$this->user_id."&amp;type=cyclass&amp;cid=".$cate['SellerClassID']."'>".$info['SellerClassName']."</a>|";
	                $i++;
	            }
	        }
	    }
	    $content .= "<br/>";
	    
	    //收藏商家    
	    $pagesize = 10;
	    $displaypage = 5;
	    $page = $this->page;
	    
	    if($this->type == 'all'){
            $current_url = $_SERVER['PHP_SELF']."?act=favor&amp;uid=".$this->user_id."&amp;type=".$this->type;
            $arr_join[] = array('table'=>'fun_seller', 'table_short'=>'b', 'on'=>'a.`SellerID`=b.`SellerID`');
            $arr_filter[] = array('attr'=>'UserID','value'=>$this->user_id);
            $arr_orderby = "";
            $arr_limit = array('page' => $page, 'pagesize' => $pagesize, 'url' => ''); 
        }elseif($this->type == 'topclass'){
            $current_url = $_SERVER['PHP_SELF']."?act=favor&amp;uid=".$this->user_id."&amp;type=".$this->type."&amp;cid=".$this->cid;
            $this->cid = $this->params['cid'];
            //获取子类
            $arr_filter[] = array('attr'=>'SellerClassOwner','value'=>$this->cid);
            $all_subcate = $this->core->load('seller::sellerBase')->getSellerCateList($arr_filter,'','');
            if($all_subcate['list']){
                foreach($all_subcate['list'] as $cate){
                    $arr_subcateid[] = $cate['SellerClassID'];
                }
            }
            $str_subcateids = @implode(',', $arr_subcateid);
            
            $arr_filter = array();
            $arr_join[] = array('table'=>'fun_seller', 'table_short'=>'b', 'on'=>'a.`SellerID`=b.`SellerID`');
            $arr_join[] = array('table'=>'fun_seller_class', 'table_short'=>'c', 'on'=>' FIND_IN_SET(c.`SellerClassID`, b.`SellerClassID`)');
            $arr_filter[] = array('attr'=>'UserID','value'=>$this->user_id);
            $arr_filter[] = array('join'=>'b','attr'=>'SellerClassID','value'=>$str_subcateids,'match'=>'in');
            $arr_orderby = "";
            $arr_limit = array('page' => $page, 'pagesize' => $pagesize, 'url' => ''); 
            $arr_groupby[] = 'SellerID';
        }elseif($this->type == 'area'){
            $current_url = $_SERVER['PHP_SELF']."?act=favor&amp;uid=".$this->user_id."&amp;type=".$this->type."&amp;aid=".$this->aid;
            $this->aid = $this->params['aid'];
            $arr_join[] = array('table'=>'fun_seller', 'table_short'=>'b', 'on'=>'a.`SellerID`=b.`SellerID`');
            $arr_join[] = array('table'=>'fun_seller_area', 'table_short'=>'c', 'on'=>'b.`SellerAreaID`=c.`SellerAreaID`');
            $arr_filter[] = array('attr'=>'UserID','value'=>$this->user_id);
            $arr_filter[] = array('join'=>'c','attr'=>'SellerAreaID','value'=>$this->aid,'match'=>'=');
            $arr_orderby = "";
            $arr_limit = array('page' => $page, 'pagesize' => $pagesize, 'url' => ''); 
            $arr_groupby = "";
        }elseif($this->type == 'cbd'){
            $current_url = $_SERVER['PHP_SELF']."?act=favor&amp;uid=".$this->user_id."&amp;type=".$this->type."&amp;cid=".$this->cid;
            $this->cid = $this->params['cid'];
            $arr_join[] = array('table'=>'fun_seller', 'table_short'=>'b', 'on'=>'a.`SellerID`=b.`SellerID`');
            $arr_join[] = array('table'=>'fun_seller_cbd', 'table_short'=>'c', 'on'=>'b.`SellerCBDID`=c.`SellerCBDID`');
            $arr_filter[] = array('attr'=>'UserID','value'=>$this->user_id);
            $arr_filter[] = array('join'=>'c','attr'=>'SellerCBDID','value'=>$this->cid,'match'=>'=');
            $arr_orderby = "";
            $arr_limit = array('page' => $page, 'pagesize' => $pagesize, 'url' => ''); 
            $arr_groupby = "";
        }elseif($this->type == 'cyclass'){
            $current_url = $_SERVER['PHP_SELF']."?act=favor&amp;uid=".$this->user_id."&amp;type=".$this->type."&amp;cid=".$this->cid;
            $this->cid = $this->params['cid'];
            
            
            $arr_join[] = array('table'=>'fun_seller', 'table_short'=>'b', 'on'=>'a.`SellerID`=b.`SellerID`');
            $arr_join[] = array('table'=>'fun_seller_class', 'table_short'=>'c', 'on'=>' FIND_IN_SET(c.`SellerClassID`, b.`SellerClassID`)');
            $arr_filter[] = array('attr'=>'UserID','value'=>$this->user_id);
            $arr_filter[] = array('join'=>'c','attr'=>'SellerClassID','value'=>$this->cid,'match'=>'=');
            $arr_orderby = "";
            $arr_limit = array('page' => $page, 'pagesize' => $pagesize, 'url' => ''); 
            $arr_groupby[] = 'SellerID';
        }

        $counts = $this->core->load('user::userBase')->getInfoList(array('table'=>'fun_user_favorites', 'primary'=>'UserFavId'), $arr_join, $arr_filter, $arr_orderby, '', true, 'a.SellerID');

        $totalPages = ceil($counts/$pagesize);

        $pager = $this->core->utility->wap_pager($totalPages, $page, $displaypage, $current_url);
        $content .= $pager ."<br/>";
        
        
        $favors = $this->core->load('user::userBase')->getInfoList(array('table'=>'fun_user_favorites', 'primary'=>'UserFavId'), $arr_join, $arr_filter, $arr_orderby, $arr_limit, false, 'a.SellerID', $arr_groupby); 
   
        if($favors['list']){
            foreach($favors['list'] as $favor){
                $content .= "<a href='./mycity.php?act=sellerinfo&amp;uid=".$this->user_id."&amp;sid=".$favor['SellerID']."'>".$favor['SellerNameCN']."</a><br/>";
            }
        }
        
        $content .= $pager."<br/>";

	    $content .= "<br/><br/>";
	    $content .= "<a href='' alt='建议帮助'>建议帮助</a>";

	    $this->core->tpl->assign('content', $content);
	    $this->core->tpl->assign('page_title', $page_title);
	    $this->core->tpl->display('comm.wml');
    }

    function run_showinfo(){
        $mid = $this->params['mid'];
        $page_title = "我的家园-短消息";
        $backurl = $_SERVER['PHP_SELF'] . '?act=msg&uid=' . $this->user_id;
        $posturl = $_SERVER['PHP_SELF'] . '?act=msg&step=reply_do&uid=' . $this->user_id . '&mid=' . $mid;
	    $content = "";
        
        if(!$mid){
            $this->error = "对不起，您查看的短消息不存在！";
            $this->jumpError("", $page_title, $backurl);
            exit;
        }
        $tag = $this->getUserMsg($mid, '', '', $this->typecode, '', true);
        if(!$tag){
            $this->error = "对不起，您查看的短消息不存在！";
            $this->jumpError("", $page_title, $backurl);
            exit;
        }

        //获取短消息内容
        $info = $this->getUserMsg($mid, '', '', $this->typecode);
        $sender = $this->core->load('user::userBase')->getInfo(array('table'=>'fun_user', 'primary'=>'UserID'), $info['PosterID']);

        $content .= "<a href='".$backurl."'>返回上级</a><br/><br/>";
        if($this->type == 'msg'){
            $content .= "<input type='text' name='msg_replay' value='' maxlength='20' size='15' />";
            $content .= "<anchor title=\"回复\">回复<go href=\"".$posturl."\" method=\"post\">
                        <postfield name=\"type\" value=\"msg\" />
                        <postfield name=\"msg_replay\" value=\"$(msg_replay)\" />
                        <postfield name=\"receiver_id\" value=\"".$info['PosterID']."\" />
                        </go></anchor><br/>";


            $content .= $sender['UserNickName'] . " " .$info['Sendtime'] ."<br/>";
            $content .= $info['MsgContent'] . "<br/>";

            $content .= "<input type='text' name='msg_replay' value='' maxlength='20' size='15' />";
            $content .= "<anchor title=\"回复\">回复<go href=\"".$posturl."\" method=\"post\">
                        <postfield name=\"type\" value=\"msg\" />
                        <postfield name=\"msg_replay\" value=\"$(msg_replay)\" />
                        <postfield name=\"receiver_id\" value=\"".$info['PosterID']."\" />
                        </go></anchor><br/>";
        }else{
            $content .= $info['Sendtime'] . "<br/>";
            $content .= $info['MsgContent'] . "<br/>";
        }

        $content .= "<br/><br/>";
        $content .= "<a href='".$backurl."'>返回上级</a><br/>";
	    $content .= "<a href='' alt='建议帮助'>建议帮助</a>";

	    $this->core->tpl->assign('content', $content);
	    $this->core->tpl->assign('page_title', $page_title);
	    $this->core->tpl->display('comm.wml');
        
    }

    function run_reply_do(){
        $mid = $this->params['mid'];
        $page_title = "我的家园-短消息";
        $backurl = $_SERVER['PHP_SELF'] . '?act=msg&uid=' . $this->user_id;
	    $content = "";

        if(!$mid){
            $this->error = "对不起，您回复的短消息不存在！";
            $this->jumpError("", $page_title, $backurl);
            exit;
        }
        $tag = $this->getUserMsg($mid, '', '', $this->typecode, '', true);
        if(!$tag){
            $this->error = "对不起，您回复的短消息不存在！";
            $this->jumpError("", $page_title, $backurl);
            exit;
        }
        $receiver_id = $this->params['receiver_id'];
        $msgcontent = htmlspecialchars($this->params['msg_replay']);
        $sql = "INSERT INTO `fun_msg` SET `PosterID`='".$this->user_id."', `ReceiverID`='".$receiver_id."', `MsgContent`='".$msgcontent."', `Sendtime`=now(), `Msgtype`='1', `Isread`='1'";
        $res = $this->core->db->query($sql);
        if($res){
            $content = "回复成功！";
        }else{
            $content = "回复失败！";
        }

    	$this->jumpPrompt($content, $page_title, $backurl);
    }
    

}


?>
